package Exams.MeituanPack.q031205_goodNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    static int[] nums1 = new int[]{1,0, 1 ,0 ,1, 0};
    static int[] nums2 = new int[]{0 ,1, 2, 1, 4, 4};
    static int black = 0,white = 0;
    public static void main(String[] args) {
        Map<Integer, List<Integer>> root = new HashMap<>();
        for (int i = 0; i < nums2.length; i++) {
            root.put(i, new ArrayList<>());
        }
        for (int i = 1; i < nums2.length; i++) {
            int fatherIndex = nums2[i] - 1;
            List<Integer> value = root.get(fatherIndex);
            value.add(i);
            root.put(fatherIndex, value);
        }
        dfs(0, root);
        System.out.println(black + "," + white);
    }
    public static void dfs(int root, Map<Integer, List<Integer>> temp) {
        //没有子节点
        if(temp.get(root).size() == 0) {
            if (nums1[root] == 0) {
                white++;
            } else {
                black++;
            }
            return;
        }
        int sonBlack = 0;
        //不是叶子结点，挨个深度遍历到其子节点
        for (Integer node : temp.get(root)) {
            dfs(node,temp);
            //统计子节点黑色数量
            if (nums1[node] == 1) {
                sonBlack++;
            }
        }
        if (nums1[root] == 1 && sonBlack == 0 ) black++;
        if (nums1[root] == 0 && sonBlack >= 1) white++;
    }

}
